package com.lora.web;

import com.acooly.core.common.web.support.JsonListResult;
import com.acooly.core.utils.Servlets;
import com.lora.service.RegisterService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/manage/demo/register")
public class RegisterController {
    @Resource
    RegisterService registerService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @ResponseBody
    @RequestMapping("save")
    public JsonListResult<Map<String, Object>> save(HttpServletRequest request){
        JsonListResult<Map<String, Object>> result=new JsonListResult<>();
        Map<String, Object> map = Servlets.getParametersStartingWith(request, "");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("creattime",sdf.format(new Date()));
        try {
            int i=registerService.save(map);
            if(i>0){
                result.setSuccess(true);
            }else {
                result.setSuccess(false);
            }
        }catch (Exception e){
            result.setSuccess(false);
        }

        return result;
    }
    @RequestMapping("registerEdit")
    public String  registerEdit(HttpServletRequest request, Model model){
       // List<Map<String, Object>> maps = registerService.accessList(new HashMap<>());
       // model.addAttribute("list",maps);
        return "/manage/register/registerEdit";
    }
    @RequestMapping("register")
    public String  register(HttpServletRequest request){
        return "/manage/register/register";
    }
    /**
     * 登记查询
     * @return
     */
    @RequestMapping(value = "queryList")
    @ResponseBody
    public JsonListResult<Map<String, Object>> queryList(HttpServletRequest request){
        JsonListResult<Map<String, Object>> result=new JsonListResult<>();
        Map<String,Object> map=new HashMap<>();
        String types=request.getParameter("equipment");
        String page=request.getParameter("page");
        String rows=request.getParameter("rows");
        String equipmenttype="";
        if(!StringUtils.isBlank(types)){
            String[] t=types.split("_");
            equipmenttype=t[0];
        }
        map.put("equipmenttype",equipmenttype);
        map.put("order","creattime");
        map.put("page",page);
        map.put("rows",rows);
        List<Map<String, Object>> query = registerService.queryList(map);
        result.setRows(query);
        result.setTotal(Long.valueOf(query.size()));
        return result;
    }
    @RequestMapping(value = "delete")
    @ResponseBody
    public Map<String,Object> delete(HttpServletRequest request){

        String id=request.getParameter("id");
        jdbcTemplate.update("DELETE FROM `register` where id="+id);
        return null;

    }
}
